﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Controls
{
    public class PsDataGridView : DataGridView
    {
        private List<string> editCols = null;

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            if (msg.WParam.ToInt32() == (int)Keys.Enter) // 
            {
                if (this.IsCurrentCellInEditMode) //如果正在编辑，则先结束编辑
                {
                    this.EndEdit();
                }

                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            _isPressDataGridView = true;
            base.OnKeyPress(e);
        }

        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            _isPressDataGridView = true;
            base.OnPreviewKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            _isPressDataGridView = false;
        }

        //由于控件在被调用时，在Key_up方法中，如果是由其他的控件Key_down, 然后再KeySends到GridView上面来的话，也会触发GridView的KeyUP事件
        private bool _isPressDataGridView = false;

        /// <summary>
        /// 是不是在GridView上面触发的KeyDown事件引起的KeyUp事件
        /// </summary>
        public bool IsPressDataGridView
        {
            get { return _isPressDataGridView; }
            set { _isPressDataGridView = value; }
        }

        public object GetValue(string name)
        {
            if (!this.Columns.Contains(name) || null == this.CurrentRow) return null;

            return this.CurrentRow.Cells[name].Value;
        }

        public void SetValue(string name, object val)
        {
            if (!this.Columns.Contains(name) || null == this.CurrentRow) return;

            this.CurrentRow.Cells[name].Value = val;
        }

        /// <summary>
        /// 行选中
        /// </summary>
        /// <param name="rowindex"></param>
        public void Focus(int rowindex)
        {
            if (rowindex < 0 || rowindex > this.Rows.Count) return;

            this.Rows[rowindex].Selected = true;
            this.Focus();
        }

        /// <summary>
        /// 选中列
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="colIndex"></param>
        public void Focus(int rowIndex, int colIndex)
        {
            if (rowIndex < 0
                || rowIndex > this.Rows.Count
                || colIndex < 0
                || colIndex > this.ColumnCount
                || 0 == this.Rows.Count) return;

            this[colIndex, rowIndex].Selected = true;
            this.Focus();
        }



        /// <summary>
        /// 选中列
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="colName"></param>
        public void Focus(int rowIndex, string colName)
        {
            if (rowIndex < 0
                || rowIndex > this.Rows.Count
                || !this.Columns.Contains(colName)
                || 0 == this.Rows.Count) return;

            this[colName, rowIndex].Selected = true;
            this.Focus();
        }

        /// <summary>
        /// 选中单元格
        /// </summary>
        /// <param name="colName"></param>
        public void Focus(string colName)
        {
            int rowIndex = this.SelectedRowIndex;
            if (rowIndex > -1)
            {
                this.Focus(rowIndex, colName);
            }
            this.Focus();
        }

        /// <summary>
        /// 当前控件选中行的索引
        /// </summary>
        public int SelectedRowIndex
        {
            get
            {
                if (this.Rows.Count == 0) return -1;
                return (this.SelectedCells != null && this.SelectedCells.Count > 0) ?
                    this.SelectedCells[0].RowIndex : -1;
            }
        }

        /// <summary>
        /// 控件是否可以编辑
        /// </summary>
        public bool CanEdit
        {
            get
            {
                if (this.ReadOnly == false) return true;

                foreach (DataGridViewColumn dc in this.Columns)
                {
                    if (dc.Visible == true && dc.ReadOnly == false)
                        return true;
                }

                return false;
            }
        }

        private int _lastCanEditIndex = -1;

        /// <summary>
        /// 最后一个可以编辑的索引
        /// </summary>
        public int LastCanEditIndex { get { return _lastCanEditIndex; } }

        /// <summary>
        /// 记录可以编辑的列
        /// </summary>
        public void WriteEditCols()
        {
            if (this.ReadOnly == true) return;

            editCols = new List<string>();
            foreach (DataGridViewColumn dc in this.Columns)
            {
                if (dc.Visible == true && dc.ReadOnly == false)
                {
                    editCols.Add(dc.Name);
                    _lastCanEditIndex = dc.Index;
                }
            }
        }

        /// <summary>
        /// 得到下一个可以编辑的列
        /// </summary>
        /// <returns></returns>
        public string GetNextCanEditColomun()
        {
            if (null == this.CurrentCell) return null;
            int columnIndex = this.CurrentCell.ColumnIndex;
            if (columnIndex < 0) return null;

            string name = this.Columns[columnIndex].Name;
            return GetNextCanEditColomun(name);
        }

        /// <summary>
        /// 得到下一个可以编辑的列
        /// </summary>
        /// <param name="colName"></param>
        /// <returns></returns>
        public string GetNextCanEditColomun(string colName)
        {
            if (null == editCols) return null;

            int nowIndex = this.editCols.IndexOf(colName);
            if (nowIndex == -1) return null;
            if (nowIndex > editCols.Count - 2) //2其中的1是count和引索的差，另外一个1是我要去nowIndex的下一个
            {
                return null;
            }

            return editCols[nowIndex + 1];
        }

        /// <summary>
        /// 清空列
        /// </summary>
        public void Clear()
        {
            if (null != this.DataSource)
            {
                this.DataSource = null;
            }
            else
            {
                this.Rows.Clear();
            }
        }
    }
}
